﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Analysis;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Classify Filter
    /// </summary>
    public class ClassifyFilter : BaseImageFilter
    {
        protected IntInterval[] m_aIntInterval = new IntInterval[] { new IntInterval(20, 30), new IntInterval(40, 50), new IntInterval(120, 130) };
        protected Color[] aColor = Definitions.KnownColors;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public ClassifyFilter()
        {
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <param name="_classifyFilter"></param>
        internal ClassifyFilter(ClassifyFilter _classifyFilter)
        {
            m_aIntInterval = _classifyFilter.m_aIntInterval;
        }

        /// <summary>
        /// Gets or sets colors
        /// </summary>
        public Color[] Colors
        {
            get { return aColor; }
            set { aColor = value; }
        }

        /// <summary>
        /// Gets or sets Interval
        /// </summary>
        public IntInterval[] IntIntervall
        {
            get { return m_aIntInterval; }
            set { m_aIntInterval = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Classify classifyImage = new Classify();
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;
            classifyImage.ClassifyImage(_bitmap, nWidth, nHeight, m_aIntInterval);

            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    int nGrayValue = _bitmap.GetGrayPixelValue(x, y);
                    int nNewValue = classifyImage.ClassifyPixel(nGrayValue, m_aIntInterval);
                    _bitmap.SetPixel(x, y, aColor[nNewValue]);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ClassifyFilter(this);
        }
    }
}
